package mapReduce.homework.q6_orderInfoTop10;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

public class GetMenuSailTop10Comparator extends WritableComparator {
    public GetMenuSailTop10Comparator(){
        super(Text.class,true);
    }
    @Override
    public int compare(WritableComparable a, WritableComparable b) {

        int res = 0;

        String aBean = ((Text)a).toString();
        String bBean = ((Text)b).toString();

        String[] afields = aBean.split("\t");
        String[] bfields = bBean.split("\t");

        String ayear = afields[0];
//        String aorderId = afields[1];
        String apartner = afields[2];
        String amenuId = afields[3];
        double aprice = Double.parseDouble(afields[4]);
        int acount = Integer.parseInt(afields[5]);

        String byear = bfields[0];
//        String borderId = bfields[1];
        String bpartner = bfields[2];
        String bmenuId = bfields[3];
        double bprice = Double.parseDouble(bfields[4]);
        int bcount = Integer.parseInt(bfields[5]);

        /*

         */
        if(ayear.equals(byear)){
            if(acount==bcount){
                if(aprice==bprice){
                    if(apartner.equals(bpartner)){
                        res = amenuId.compareTo(bmenuId);
                    }else{
                        res = apartner.compareTo(bpartner);
                    }
                }else{
                    res = (int)(bprice-aprice);
                }
            }else{
                res = bcount-acount;
            }
        }else{
            res = ayear.compareTo(byear);
        }


        return res;
    }
}
